package com.ruoyi.project.user.people.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.BaseEntity;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.people.domain.TCouple;
import com.ruoyi.project.user.people.service.TCoupleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@Controller
@RequestMapping("/user/people")
public class TCoupleController  extends BaseController {
    private String prefix = "user/people";
    @Autowired
    private TCoupleService tCoupleService;

    @GetMapping()
    @RequiresPermissions("user:people:view")
    public String showPeople(){
        return prefix+"/people";
    }

    //多条件查询
    @RequiresPermissions("user:people:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findMore(TCouple tCouple){
        startPage();
        List<TCouple> list = tCoupleService.findMore(tCouple);
        return getDataTable(list);
    }

    //展示添加页面
    @RequiresPermissions("user:people:add")
    @GetMapping("/add")
    public String showAdd(){
        return prefix+"/add";
    }

    //验证电话号是否已存在
    @RequiresPermissions("user:people:add")
    @PostMapping("/checkPhone")
    @ResponseBody
    public Long checkedPhone(String couplePhone){
        return tCoupleService.checkedPhone(couplePhone);
    }

    //验证邮箱是否已存在
    @RequiresPermissions("user:people:add")
    @PostMapping("/checkEmail")
    @ResponseBody
    public Long checkedEmail(String coupleEmail){
        return tCoupleService.checkedEmail(coupleEmail);
    }

    //添加新人
    @RequiresPermissions("user:people:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addTCouple(TCouple tCouple,String couplePassword){
        return toAjax(tCoupleService.add(tCouple,couplePassword));
    }

    //展示修改页面，数据回显
    @RequiresPermissions("user:people:edit")
    @GetMapping("/edit/{coupleId}")
    public String showById(@PathVariable Long coupleId, Model model){
        System.out.println("--------"+coupleId);
        model.addAttribute("people",tCoupleService.showById(coupleId));
        return prefix+"/edit";
    }

    //修改新人信息
    @RequiresPermissions("user:people:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editPeople(TCouple tCouple){
        return toAjax(tCoupleService.updatePeopleById(tCouple));
    }

    //多条件删除新人
    @RequiresPermissions("user:people:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removePeople(Long[] ids){
        return toAjax(tCoupleService.deletePeople(ids));
    }


    //账号启用禁用
    @RequiresPermissions("user:people:state")
    @PostMapping("/updateState")
    @ResponseBody
    public AjaxResult updatestate(TCouple tCouple){
        return toAjax(tCoupleService.updateState(tCouple));
    }


    @RequiresPermissions("user:people:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TCouple tCouple) {
        List<TCouple> list = tCoupleService.findMore(tCouple);
        ExcelUtil<TCouple> excelUtil = new ExcelUtil<>(TCouple.class);
        return excelUtil.exportExcel(list,"主持人数据");
    }

}
